﻿#region

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using CuongHanh.Context;

#endregion

namespace CuongHanh.Models
{
    public static class HtmlExtensions
    {
        public static string GetLanguageName(this HtmlHelper helper)
        {
            string languageName = HttpContext.Current.Session[AppUtil.LANGUAGE] + "";
            return languageName;
        }

        public static string GetLanguageName(this HttpServerUtilityBase httpServerUtilityBase)
        {
            string languageName = HttpContext.Current.Session[AppUtil.LANGUAGE] + "";
            return languageName;
        }

        public static string GetResource(this HtmlHelper helper,
                string key)
        {
            string languageName = "Resources";
            string locate = HttpContext.Current.Session[AppUtil.LANGUAGE] + "";
            if(!string.IsNullOrEmpty(locate))
                languageName = string.Format("{0}_{1}",
                        languageName,
                        locate);
            return HttpContext.GetGlobalResourceObject(languageName,
                    key) + "";
        }

        /// <summary>
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="language">empty: default language</param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetResource(this HtmlHelper helper,
                string language,
                string key)
        {
            string languageName = "Resources";
            if("en".Equals(language))
                language = "";
            if(!string.IsNullOrEmpty(language))
                languageName = string.Format("{0}_{1}",
                        languageName,
                        language);
            return HttpContext.GetGlobalResourceObject(languageName,
                    key) + "";
        }

        /// <summary>
        ///     Get localization value by current language
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="json">{vi: "Biển số", en: "Plate"}</param>
        /// <returns></returns>
        public static string GetLocalizationValue(this HtmlHelper helper,
                string json)
        {
            string lang = HttpContext.Current.Session[AppUtil.LANGUAGE] as string;
            if(string.IsNullOrEmpty(lang))
                lang = "en";
            Dictionary<string, string> obj = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
            if(obj == null)
                return json;
            return obj[lang];
        }

        public static string GetResource(this HttpServerUtilityBase httpServerUtilityBase,
                string key)
        {
            if(string.IsNullOrEmpty(key))
                return String.Empty;

            string languageName = "Resources";
            string locate = HttpContext.Current.Session[AppUtil.LANGUAGE] + "";
            if(!string.IsNullOrEmpty(locate))
                languageName = string.Format("{0}_{1}",
                        languageName,
                        locate);
            return HttpContext.GetGlobalResourceObject(languageName,
                    key) + "";
        }

        public static string GetResource(this HttpServerUtility httpServerUtility,
                string key)
        {
            if(string.IsNullOrEmpty(key))
                return String.Empty;

            string languageName = "Resources";
            string locate = HttpContext.Current.Session[AppUtil.LANGUAGE] + "";
            if(!string.IsNullOrEmpty(locate))
                languageName = string.Format("{0}_{1}",
                        languageName,
                        locate);
            return HttpContext.GetGlobalResourceObject(languageName,
                    key) + "";
        }

        public static string GetResource(string key)
        {
            if(string.IsNullOrEmpty(key))
                return String.Empty;

            string languageName = "Resources";
            string locate = HttpContext.Current.Session["language"] + "";
            if(!string.IsNullOrEmpty(locate))
                languageName = string.Format("{0}_{1}",
                        languageName,
                        locate);
            return HttpContext.GetGlobalResourceObject(languageName,
                    key) + "";
        }

        //public static bool CheckPermission(this HtmlHelper helper,
        //                                   string key)
        //{
        //    return AppUtil.CheckPermission(helper.ViewContext.RequestContext.HttpContext.Application,
        //                                   helper.ViewContext.RequestContext.HttpContext.User.Identity.Name,
        //                                   key);
        //}
        public static List<int> GetListVehicle(string vehicle)
        {
            string[] s = (vehicle + "").Split(',');
            List<int> vehicles = new List<int>();
            foreach (string s1 in s)
            {
                int i;
                if(int.TryParse(s1,
                        out i))
                {
                    vehicles.Add(i);
                }
            }
            return vehicles;
        }
    }
}
